﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dycox.Data;
using Dycox.Linq;

namespace Dycox.Workflows.Data
{
    public class DataArguments
    {
        internal DataArguments()
            : this(null)
        {

        }

        internal DataArguments(object dataSource)
        {
            _dataSource = dataSource;
        }

        private object _dataSource;
        /// <summary>
        /// If specified, all data manipulation operation will be called in this context.
        /// </summary>
        public object DataSource
        {
            get { return _dataSource; }
        }

        internal DataProvider CreateProvider()
        {
            DataProvider provider;

            if (_dataSource != null)
            {
                provider = DataProvider.CreateProvider(_dataSource);
                provider.ShouldDisposeDataSource = false;
            }
            else
                provider = DataProvider.CreateProvider();

            provider.SubmittingChanges += provider_SubmittingChanges;
            return provider;
        }

        void provider_SubmittingChanges(object sender, EventArgs e)
        {
            OnProviderSubmittingChanges(e);
        }

        public event EventHandler ProviderSubmittingChanges;

        protected virtual void OnProviderSubmittingChanges(EventArgs e)
        {
            if (ProviderSubmittingChanges != null)
                ProviderSubmittingChanges(this, e);
        }

        private LoadReferences _loadReferences;

        public LoadReferences LoadReferences
        {
            get { return _loadReferences; }
            set { _loadReferences = value; }
        }

    }
}
